iT邦幫忙

2023 iThome 鐵人賽

DAY 12
0
SideProject30

Electron Angular軟體架構與簡易功能實作學習路程實記系列 第 12

Day 12- 功能規格設計-後端Api開發設計-2

  • 分享至 

  • xImage
  •  

SQLData

@Entity()
export class Article {
  @PrimaryGeneratedColumn()
  IdArticle: number | undefined

  @ManyToOne(() => User, (user) => user.Articles)
  user: User | undefined

  @Column()
  Title: string | undefined

  @Column()
  ArticleType: string | undefined

  @Column()
  Position: string | undefined

  @Column()
  Description: string | undefined

  @Column()
  PlayedTime: Date | undefined

  @CreateDateColumn()
  CreatedTime: Date | undefined

}

DAO

async findArticle(idArticle: number) {
    return await this.dataSource.manager
      .findBy(Article, { IdArticle: idArticle });
  }

  async saveArticle(article: any) {
    return await this.dataSource.manager.save(Article, article);
  }

  async deleteArticle(idArticle: number) {
    return await this.dataSource.getRepository(Article)
      .delete({IdArticle: idArticle});
  }

Api

ArticleController.get('/Article/getArticle/:IdArticle', (req, res) => {
  if (req.params == null)
  {
    res.status(400).send({status:"body error"});
    return;
  }
  let IdArticle = parseInt(req.params["IdArticle"]) ? parseInt(req.params["IdArticle"]) : 0;
  articleDao.findArticle(IdArticle).then((articles: Array<Article>) => {
    if (articles.length > 0)
    {
      let article:Article = articles[0];
      res.status(200).send(article);
    }
    else
    {
      res.status(400).send({status:"article not found"});
    }
  });
});

ArticleController.post('/Article/saveArticle', (req, res) => {
  if (req.body == null)
  {
    res.status(400).send({status:"body error"});
    return;
  }
  articleDao.saveArticle(req.body).then((data: any) => {
    res.status(200).send({status:"article save OK"});
  });
});

ArticleController.delete('/Article/deleteArticle/:IdArticle', (req, res) => {
  let idArticle = parseInt(req.params["IdArticle"]) ? parseInt(req.params["IdArticle"]) : 0;
  articleDao.deleteArticle(idArticle).then((data: any) => {
    res.status(200).send({status:"article delete OK"});
  });
});

小結

本日完成基本底層資料存取實作,後續針對資料連結部分進行調整和圖檔上傳部分進行開發.


上一篇
Day 11- 功能規格設計-後端Api開發設計-1
下一篇
Day 13- 功能規格設計-後端Api開發設計-3
系列文
Electron Angular軟體架構與簡易功能實作學習路程實記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言